import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
from sklearn.datasets import make_blobs
from sklearn_extra.cluster import KMedoids
import numpy as np

plt.figure(figsize=(12, 12))

n_samples = 1500
x, y = make_blobs(n_samples=n_samples, random_state=170)

plt.subplot(331)
plt.scatter(x[:,0],x[:,1], c=y)
plt.title("original")

y_pred = KMeans(n_clusters=3, random_state=170).fit_predict(x)

plt.subplot(332)
plt.scatter(x[:,0],x[:,1], c=y_pred)
plt.title("Pred")

y_pred2 = KMeans(n_clusters=2, random_state=170).fit_predict(x)
plt.subplot(333)
plt.scatter(x[:,0],x[:,1], c=y_pred2)
plt.title("Pred 2")


trans = [[0.6083, -0.6366],[-0.4088,0.8525]]
x_trans = np.dot(x, trans)

plt.subplot(334)
plt.scatter(x_trans[:,0],x_trans[:,1], c=y)
plt.title("trans")

y_pred_trans = KMeans(n_clusters=3, random_state=170).fit_predict(x_trans)
plt.subplot(335)
plt.scatter(x_trans[:,0],x_trans[:,1], c=y_pred_trans)
plt.title("trans predict")


x_varied, y_varied = make_blobs(n_samples=n_samples, cluster_std=[1.0,2.5,0.5],random_state=170)
plt.subplot(337)
plt.scatter(x_varied[:,0],x_varied[:,1], c=y_varied)
plt.title("Original Varied")

y_pred_varied = KMeans(n_clusters=3, random_state=170).fit_predict(x_varied)
plt.subplot(338)
plt.scatter(x_varied[:,0],x_varied[:,1], c=y_pred_varied)
plt.title("Predict Varied")


y_med_trans = KMedoids(n_clusters=3,method="pam",init="build").fit_predict(x_trans)
plt.subplot(336)
plt.scatter(x_trans[:,0],x_trans[:,1], c=y_med_trans)
plt.title("trans med predict")

y_med_varied = KMedoids(n_clusters=3,method="pam",init="build").fit_predict(x_varied)
plt.subplot(339)
plt.scatter(x_varied[:,0],x_varied[:,1], c=y_med_varied)
plt.title("Predict med Varied")

from sklearn import metrics
score = metrics.adjusted_rand_score(y_varied, y_pred_varied)
score2 = metrics.adjusted_rand_score(y_varied, y_med_varied)
print(score)
print(score2)

score3 = metrics.adjusted_mutual_info_score(y_varied, y_pred_varied)
score4 = metrics.adjusted_mutual_info_score(y_varied, y_med_varied)
print(score3)
print(score4)

score5 = metrics.silhouette_score(x_varied, y_pred_varied)
score6 = metrics.silhouette_score(x_varied, y_med_varied)
print(score5)
print(score6)

plt.savefig('cluster.png')